package com.letter.otools.day;

import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.letter.otools.day.util.AppUtils;
import com.letter.otools.day.util.StatusBarUtils;


public abstract class BaseActivity extends AppCompatActivity {
    protected abstract int getLayout();

    protected abstract void initView();

    protected void setSupportToolbarBack(Toolbar toolbar) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); // 启用返回按钮
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(v -> {
            onBackPressed();
        });
    }

    protected void initBinder() {
    }

    protected void initData() {
    }

    protected Activity getActivity() {
        return this;
    }

    protected ActivityResultLauncher<Intent> resultLauncher = null;

    protected void onResultData(int resultCode, Intent intent) {
    }

    protected void onLunchResult(ActivityResult result) {
        onResultData(result.getResultCode(), result.getData());
    }

    protected void launchForResult(Intent intent) {
        if (resultLauncher != null) {
            resultLauncher.launch(intent);
        }
    }

    protected void launchForResult(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        if (resultLauncher != null) {
            resultLauncher.launch(intent);
        }
    }

    protected void registerForActivityResult() {
        resultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    onLunchResult(result);
                });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initData();
        super.onCreate(savedInstanceState);
        if (getLayout() != 0) {
            setContentView(getLayout());
        }
        initBinder();
        //默认沉浸式状态栏；白底黑色
//        showTranslateDarkStatusBar();
//        this.getWindow().getDecorView().setSystemUiVisibility(AppUtils.getAppNightMode() == MODE_NIGHT_YES ? View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
//                : View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        if (AppUtils.getAppNightMode() == MODE_NIGHT_YES) {
            StatusBarUtils.setStatusBarDarkMode(this);
        } else {
            StatusBarUtils.setStatusBarLightMode(this);
        }
        StatusBarUtils.setNavigationBarColor(this, AppUtils.getAppNightMode() == MODE_NIGHT_YES ? Color.TRANSPARENT : Color.WHITE);

        initView();
        initEnd();
    }

    //初始化完成后回调
    protected void initEnd() {
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (unBinder != null) {
//            unBinder.unbind();
//        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


//    protected void showWhiteStatusBar() {
//        ImmersionBar.with(this)
//                .statusBarColor(R.color.white)
//                .statusBarDarkFont(true)   //状态栏字体是深色，不写默认为亮色
//                .init();
//    }
//
//
//    protected void showTranslateDarkStatusBar() {
//        ImmersionBar.with(this)
//                .statusBarColor(R.color.transparent)
//                .statusBarDarkFont(true)   //状态栏字体是深色，不写默认为亮色
//                .navigationBarDarkIcon(true)
//                .navigationBarColor(R.color.white)
//                .init();
//    }
}
